package tpase.client.Handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Pairing;
import tpase.client.VO.Message;
import tpase.client.VO.StatusCode;
import tpase.client.VO.VerifyInfo;
import tpase.util.Util;

import java.util.List;
import java.util.Map;

public class ClientUploadFileHandler extends ChannelInboundHandlerAdapter {
    private String uid;
    private String fileName;
    private String type;
    private byte[] ciphertextBytes;

    public ClientUploadFileHandler(String uid, String fileName, String type, byte[] ciphertextBytes) {
        this.uid = uid;
        this.fileName = fileName;
        this.type = type;
        this.ciphertextBytes = ciphertextBytes;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Message request = new Message(StatusCode.UPLOAD_FILE_REQUEST);
        Map<String, byte[]> map = request.getMap();
        map.put("uid", uid.getBytes());
        map.put("fileName", fileName.getBytes());
        map.put("type", type.getBytes());
        map.put("ciphertext", ciphertextBytes);

        ctx.writeAndFlush(request);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Message respond = (Message) msg;
        int statusCode = respond.getStatusCode();
        if (statusCode == StatusCode.UPLOAD_FILE_RESPONSE) {
            uploadFileResponse(ctx, respond);
            ctx.close();
        }
    }

    private void uploadFileResponse(ChannelHandlerContext ctx, Message respond) {
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("ClientKGHandler: 发生异常，关闭通道");
        System.out.println(cause.toString());
        ctx.close();
    }
}